package blink.guimodel;

import javax.swing.AbstractListModel;
import blink.model.Task;
import blink.model.TaskBD;

public class TaskListModel extends AbstractListModel {

	private TaskBD taskBD;
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public TaskListModel(TaskBD tbd) {
		taskBD = tbd;
	}

	public Object getElementAt(int i) {
		return taskBD.getTask(i);
	}

	public int getSize() {
		return taskBD.getSize();
	}

	public void createNewTask() {
		int size = getSize();
		taskBD.addOrReplaceTask(new Task());
		this.fireIntervalAdded(this, size, size);
	}

	public void replaceTask(int idx, Task modifiedTask) {
		taskBD.addOrReplaceTask(modifiedTask);
		fireContentsChanged(idx);
	}

	public void removeTask(int idx) {
		if (idx != -1) {
			taskBD.removeTask((Task) getElementAt(idx));
			this.fireIntervalRemoved(this, idx, idx);
		}
	}

	public void fireContentsChanged(int selectedIndex) {
		fireContentsChanged(this, selectedIndex, selectedIndex);
	}

}
